/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.commands.pool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.fireflow.pdl.fpdl20.diagram.LaneShape;
import org.fireflow2.designer.eclipse.commands.CommonResizeCommand;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.Wrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.LaneWrapper;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class PoolResizeCommand extends CommonResizeCommand {    
	Map<String,Integer> oldLaneWidthMap = new HashMap<String,Integer>();
	Map<String,Integer> oldLaneHeightMap = new HashMap<String,Integer>();
	int dWidth = 0;
	int dHeight =0 ;
	
	public void setModelWrapper(NodeWrapper wrapper){
		this.modelWrapper = wrapper;
	}

	
	public void execute() {
		NodeWrapper nodeWrapper = (NodeWrapper)modelWrapper;
		this.oldRect = nodeWrapper.getBox();
		newRect = new Rectangle(oldRect);
		newRect.resize(sizeDelta);
		
		nodeWrapper.setBox(newRect);
		
		dWidth = newRect.width-oldRect.width;
		dHeight = newRect.height-oldRect.height;
		
		//改变内部Lane的尺寸
		List<Wrapper> laneWrapperList = nodeWrapper.getChildren();
		for (Wrapper wrapper : laneWrapperList ){
			LaneWrapper laneWrapper = (LaneWrapper)wrapper;
			LaneShape laneShape = (LaneShape)laneWrapper.getDiagramElement();
			org.fireflow.pdl.fpdl20.diagram.basic.Rectangle fireRect = (org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)laneShape.getShape();
			
			int oldWidth = fireRect.getBounds().getWidth();
			oldLaneWidthMap.put(laneShape.getId(), fireRect.getBounds().getWidth());
			oldLaneHeightMap.put(laneShape.getId(),fireRect.getBounds().getHeight());
			
			Rectangle rect = new Rectangle();
			rect.x = fireRect.getBounds().getUpperLeftCorner().getX();
			rect.y = fireRect.getBounds().getUpperLeftCorner().getY();
			rect.height = fireRect.getBounds().getHeight();//TODO 高度自动调整方案待研究
			rect.width = oldWidth+dWidth;
			
			laneWrapper.setBox(rect);
		}
	}
	
	public void redo(){
		NodeWrapper nodeWrapper = (NodeWrapper)modelWrapper;
		nodeWrapper.setBox(newRect);		
		//改变内部Lane的尺寸
		List<Wrapper> laneWrapperList = nodeWrapper.getChildren();
		for (Wrapper wrapper : laneWrapperList ){
			LaneWrapper laneWrapper = (LaneWrapper)wrapper;
			LaneShape laneShape = (LaneShape)laneWrapper.getDiagramElement();
			org.fireflow.pdl.fpdl20.diagram.basic.Rectangle fireRect = (org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)laneShape.getShape();
			
			int oldWidth = oldLaneWidthMap.get(laneShape.getId());

			Rectangle rect = new Rectangle();
			rect.x = fireRect.getBounds().getUpperLeftCorner().getX();
			rect.y = fireRect.getBounds().getUpperLeftCorner().getY();
			rect.height = fireRect.getBounds().getHeight();//TODO 高度自动调整方案待研究
			rect.width = oldWidth+dWidth;
			
			laneWrapper.setBox(rect);
		}
	}
	
	public void undo(){
		NodeWrapper nodeWrapper = (NodeWrapper)modelWrapper;
		nodeWrapper.setBox(oldRect);
		
		//恢复内部Lane的尺寸
		List<Wrapper> laneWrapperList = nodeWrapper.getChildren();
		for (Wrapper wrapper : laneWrapperList ){
			LaneWrapper laneWrapper = (LaneWrapper)wrapper;
			LaneShape laneShape = (LaneShape)laneWrapper.getDiagramElement();
			org.fireflow.pdl.fpdl20.diagram.basic.Rectangle fireRect = (org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)laneShape.getShape();
			
			int oldWidth = oldLaneWidthMap.get(laneShape.getId());
			
			Rectangle rect = new Rectangle();
			rect.x = fireRect.getBounds().getUpperLeftCorner().getX();
			rect.y = fireRect.getBounds().getUpperLeftCorner().getY();
			rect.height = fireRect.getBounds().getHeight();//TODO 高度自动调整方案待研究
			rect.width = oldWidth;
			
			laneWrapper.setBox(rect);
		}
	}
}
